
import {rsaPrivateData} from '@/pagesH5/utils/utils.js'
// 画布基本配置
function getRelation(relation) {
    const arr =['#FB793F','#84A7FF','#1BA44E','#CED7FF']
    // const arr =['red','blue','yellow','green']
    // const index = Math.floor((Math.random()*arr.length));
    // const arr =['#CED7FF','#84A7FF','#1BA44E','#FB793F']
    // const index = Math.floor((Math.random()*arr.length));
    // let _color;
    // switch (relation) {
    //     case '企业':
    //         _color = '#84A7FF'
    //         break;
    //     case '好友':
    //         _color = '#1BA44E'
    //         // _color = '#1f375c'
    //         // _color = 'rgba(255,255,255,.1)'
    //         break;
    //     case '同事':
    //         _color = '#FB793F'
    //         break;
    //     default:
    //         _color = '#CED7FF'
    //         break;
    // }

    return arr[relation]
}
export default class Module {
    constructor(data) {
        if(!data){ return}
        this.rootId = rsaPrivateData(data[0].id)
        this.users = {};
        this.links = [];
        this.heads = [];
        this.initParam(data)
    }
    initParam(data){
        // console.log(data[0])
        // console.log(this.users)
        this.updateData(data)
        // console.log(this.users)
        // console.log(this.links)
        // if(this.rootId){
        //     // if(parent && parent.friendTotal > 7){
        //     const id = this.rootId + 'aa'
        //     const curUser = this.users[this.rootId]
        //     this.users[id] =   this.userMore(this.rootId, id, curUser.width, curUser.borderWidth,  curUser.color)
        //     this.links.push({ 'from': this.rootId , 'to': id , text: '更多', opacity: 0.9, 'data': { 'type': '更多', name:'更多'}, color: 'none' , 
        //     isHideArrow: true })
        //     // 'hsla( ' + ((this.angle + 90) / (PI / 180) + random(-opt.jitterHue, opt.jitterHue)) + ', 100%, 50%, ' + opt.orbitalAlpha / 100 + ' )'
        // }
    }
    updateData(data, parent=null) {
        data.forEach((user, index) => {
            const { target } = user;
            user.id = rsaPrivateData(user.id)
            user.name = rsaPrivateData(user.name)
            const _info = user;
            const _isInUsers = user.id in this.users;
            
            if(!_isInUsers){
                this.users[user.id] = this.setUser(user)
            }
            if (parent) {
                // 改变最初大小
                if(parent.id == this.rootId && user.id != this.rootId){
                    this.users[user.id] = this.setUser(user)
                    // const data = this.users[user.id]
                    // data.color = getRelation(1)
                    // data.borderColor = getRelation(1)
                    // this.users[user.id] = data
                }
                // const color = getRelation(user.relation)
                let state = true
                // 去除已经存在的用户
                this.links.forEach(item=>{
                    if(item.from ==  user.id && item.to == parent.id){
                        state = false
                    }
                })
                if(state){
                    let color = getRelation(0)
                    if(!parent){
                        color = getRelation(0)
                    }else{
                        if(parent.id == this.rootId && user.id != this.rootId){
                            color = getRelation(0)
                        }else{
                            color = getRelation(1)
                            // if(_isInUsers){
                            //     color = getRelation(1)
                            // }else{
                            //     color = getRelation(2)
                            // }
                        }
                    }
                    this.links.push({ 'from': parent.id , 'to': user.id , text: user.relation, 'data': { 'type': user.relation, name: user.name }, color: color, 
                    isHideArrow: true })
                }
            }
            Object.assign(_info)
            // if (target && !_isInUsers) this.updateData(target, _info)
            if (target) this.updateData(target, _info)
        });
        // if(parent && parent.friendTotal > 7){
        //     const id = parent.id + '1'
        //     const curUser = this.users[parent.id]
        //     this.users[id] =   this.userMore(parent.id, id, curUser.width/2, curUser.borderWidth,  curUser.color)
        //     this.links.push({ 'from': parent.id , 'to': id , text: '更多', opacity: 0.9, 'data': { 'type': '更多', name:'更多'}, color: curUser.color , 
        //     isHideArrow: true })
        // }
    }
    setUser(data){
		const pwidth = window.screen.width
		let pi = 1
		if(pwidth > 780){
			pi = 1.2
		}
        let wh = 36 * pi
        let borderWidth = 6
        let color = getRelation(0)
        if(data.id == this.rootId){
            color = getRelation(0)
            wh = 100 * pi
            borderWidth = 6
        }else{
            if(data.target){
                color = getRelation(1)
                wh = 80 * pi
                borderWidth = 3
            }else{
                color = getRelation(2)
                wh = 50 * pi
                borderWidth = 1
            }
        }
        return this.userAvatar(data, wh, borderWidth, color)
    }
    userAvatar(data, wh, borderWidth, color){
        // const color = getRelation(data.relation)
        const obj = {
            'id': data.id,
            'text': data.name,
            'color': color,
            'borderColor':color,
            'lineColor':color,
            'borderWidth': borderWidth,
            data: data,
            // opacity: 0.9,
            width: wh,
            height: wh,
            // 'data': { 'isGoodMan': true, 'sexType': '男' },
            'innerHTML': '<div class="c-my-node fboxRow Ycenter Xcenter" style="border-radius: 50%;overflow: hidden;width:'+wh+'px;height:'+wh+'px; background-image: url('+data.imgUrl+')">' +
                '<div class="c-node-name" style="color:#fff;font-weight:bold;font-size:'+(wh/6)+'px">'+data.name+'</div></div>'
        }
        if(!data.target){
            obj.innerHTML= '<div class="c-my-node fboxRow Ycenter Xcenter" style="border-radius: 50%;overflow: hidden;width:'+wh+'px;height:'+wh+'px; background-image: url('+data.imgUrl+')">' +
                '</div>'
        }
        if(data.isHidden){
            obj.innerHTML= '<div class="c-my-node fboxRow Ycenter Xcenter" style="border-radius: 50%;overflow: hidden;width:'+wh+'px;height:'+wh+'px;font-size:'+(wh/2)+'px;text-align:center">?</div>'
        }
        return  obj
    }
    userMore(parent, data, wh, borderWidth, color){
        const obj = {
            'id': data,
            data: {
                id: parent
            },
            'text': '更多',
            'color': 'none',
            'borderColor':'none',
            'lineColor':color,
            'borderWidth': borderWidth,
            // opacity: 0.5,
            width: wh,
            height: wh,
            // 'data': { 'isGoodMan': true, 'sexType': '男' },
            'innerHTML': '<div class="c-my-node fboxRow Ycenter Xcenter c-animation" style="width:'+wh+'px;height:'+wh+'px;"></div>'
                // 'innerHTML': '<div class="c-my-node fboxRow Ycenter Xcenter c-animation" style="width:'+wh+'px;height:'+wh+'px;">' +
                //     '<div class="c-node-name" style="color:#fff;font-weight:bold;margin-top:0; font-size:'+(wh/6)+'px">更多</div></div>'
        }
        return  obj
    }
}
